import api from '../api/api'
import { Toast, Indicator } from 'mint-ui'

let plugin = {};
plugin.install = function(Vue, options){
    Vue.prototype.$api = api;

    Vue.prototype.transferUrl = function(arr){
        arr.forEach(item => {
          item['image_url'] = api.URL + item['image_url'];
        })
    }

    Vue.prototype.transferStatus = function(arr){
        let temObj = {
            1:'未付款',
            2:'已付款',
            3:'已发货',
            4:'退货中',
            5:'退货完成',
            6:'交易完成',
            7:'交易关闭',
            8:'交易取消'
        }
        arr.forEach(item => {
            let temStr = temObj[item['order_status']];
            item['order_status'] = temStr;
        })
    }

    // 信息弹层组件
    Vue.prototype.toast = function(content){
        Toast({
            message: content,
            position: 'bottom',
            duration: 3000
        })
    }

    /**
     * @author zhangkang
     * @param {string} url 
     */
    Vue.prototype.getUrlCode = function(url){
        let reg = /code=([a-zA-Z0-9_]+)&/;
        let temStr = '';
        url.replace(reg, function($0,$1){
            temStr = $1;
        })
        return temStr;
    }

    // 标签转化
    Vue.prototype.labels = function(arr){
        let temArr = [];
        arr.forEach((item,index) => {
            let temObj = {};
            temObj.price = item.price;
            temObj.labelId = item.id;
            temObj.stock = item.stock; 
            let labels = item.norms;
            let temStr = '';
            labels.forEach((subItem,i) => {
                temStr += ` ${subItem.name}:${subItem.attributes} `;
            })
            temObj.label = temStr;
            temArr.push(temObj);
        })
        return temArr;
    }

    // 标签转换
    Vue.prototype.transferLabel = function(arr){
        arr.forEach(item => {
            let temObj = {};
            temObj.first = item.name[0];
            temObj.second = item.name[1];
            temObj.third = item.name[2];
            temObj.fourth = item.name[3];
            temObj.title = item.smallname;
            item.headTitle = temObj;
        })
    }

    // loading组件
    Vue.prototype.loading = function(content){
        if(!content){
            content = '加载中...'
        }
        Indicator.open({
            text: content,
            spinnerType: 'fading-circle'
        })
        return Indicator;
    }   

    /**
     * 发送ajax请求获取数据
     * 
     * @author zhangkang
     * @param { Object } context      : 执行上下文 
     * @param { String } method       : 发送请求的方式，get/post/put/delete
     * @param { String } api          : 发送请求的api接口
     * @param { Object } data         : 需要传输的数据,如何无则为null
     * @param { String } token        : 请求头中附加token
     * @param { Function } resHandle  : 请求返回后的处理函数
     */
    Vue.prototype.$ajax = function(context, method, api, data, token, resHandle){
        context.$axios({
            method: method,
            url: api,
            data: data,
            headers:{
                "Content-Type":"application/json",
                "token":token
            }
        }).then(function(res){
            resHandle(res);
        })
    }
}

export default plugin;